<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of UserController
 *
 * @author dejo
 */
class Admin_UserController extends Zend_Controller_Action {

    protected $_userDb;

    public function init() {
        $this->view->url = $this->_helper->mvcUrl();
        $this->_userDb = new Default_Model_DbTable_Gebruiker();
    }

    public function indexAction() {
        $this->view->users = $this->_userDb->fetchAll($this->_userDb->select()->from($this->_userDb, array('id', 'username', 'real_name', 'role')))->toArray();
    }

    public function addAction() {
        $form = $this->_helper->form('user');
        $this->view->form = $form;

        if($this->getRequest()->isPost()) {
            if($form->isValid($this->getRequest()->getPost())) {

                $values = $form->getValues();

                $data = array(
                        'id' => null,
                        'username' => $values['username'],
                        'password' => md5($values['password']),
                        'real_name' => $values['realname'],
                        'email' => $values['email'],
                        'role' => $values['role'],
                );

                $this->_userDb->insert($data);
                return $this->_helper->redirector('index');
            }
        }

    }
    public function editAction() {

        $user = $this->_userDb->find($this->getRequest()->getParam('id'))->toArray();
        $user = $user[0];
        $form = $this->_helper->form('user');
        $form->setAction($this->view->url['edit']."/id/".$this->getRequest()->getParam('id'));

        $form->removeElement('password');
        $form->removeElement('password2');

        if($this->getRequest()->isPost()) {
            $postValues = $this->getRequest()->getPost();

            if($postValues['username'] == $user['username']) {
                $userName = $form->getElement('username');
                $userName->removeValidator('Db_NoRecordExists');
            }


            if($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();

                $data = array(
                        'username' => $values['username'],
                        'real_name' => $values['realname'],
                        'email' => $values['email'],
                        'role' => $values['role'],
                );

                $this->_userDb->update($data, $this->_userDb->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam('id')));
                return $this->_helper->redirector('index');
            }
        }else {
            $userName = $form->getElement('username');
            $userName->setValue($user['username']);
            $realName = $form->getElement('realname');
            $realName->setValue($user['real_name']);
            $email = $form->getElement('email');
            $email->setValue($user['email']);
            $role = $form->getElement('role');
            $role->setValue($user['role']);
        }
        $this->view->form = $form;
    }

    public function deleteAction() {
        try{
            $this->_userDb->delete($this->_userDb->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam('id')));
        }catch(Exception $e){
            var_dump($e->getMessage());
        }
        
        return $this->_helper->redirector('index');
    }

    public function passwordAction() {

        $userId = $this->getRequest()->getParam('id', 0);

        if($userId == 0) {
            $userId = Zend_Auth::getInstance()->getStorage()->read()->id;
        }

        $form = $this->_helper->form('user');
        $form->setAction($this->view->url['password']."/id/".$userId);

        $form->removeElement('username');
        $form->removeElement('role');
        $form->removeElement('realname');
        $form->removeElement('email');

        if($this->getRequest()->isPost()) {
            if($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();

                $data = array(
                        'password' => md5($values['password'])
                );

                $this->_userDb->update($data, $this->_userDb->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam('id')));
                return $this->_helper->redirector('index');
            }
        }
        $this->view->form = $form;
    }

}
?>
